package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.context.BaseContext;
import com.sky.dto.AddDishDto;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.UpdateDishDto;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.sky.constant.MessageConstant.DISH_NAME_NOT_UNIQUE;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-25
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public  List<Dish> getDishByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.getDishByCategoryId(categoryId);

        return dishList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDish(AddDishDto dto) {
        String name = dto.getName();
        Dish dishDB  =dishMapper.selectByName(name);
        if(dishDB != null){
            throw new BaseException(DISH_NAME_NOT_UNIQUE);
        }
        Dish dish = BeanUtil.copyProperties(dto, Dish.class);
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dto.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
        if (CollectionUtil.isNotEmpty(flavors)) {
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public void updateDishStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status).setId(id);
        dishMapper.updateById(dish);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDish(UpdateDishDto dto) {
        String name = dto.getName();
        Dish dishDB  =dishMapper.selectByName(name);
        if(dishDB != null&& !Objects.equals(dishDB.getId(), dto.getId())){
            throw new BaseException(DISH_NAME_NOT_UNIQUE);
        }
        Dish dish = BeanUtil.copyProperties(dto, Dish.class);
        dishMapper.updateById(dish);
        dishFlavorMapper.deleteByDishId(dto.getId());
        List<DishFlavor> flavors = dto.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
        if (CollectionUtil.isNotEmpty(flavors)) {
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            dishMapper.deleteBatchIds(ids);
            dishFlavorMapper.deleteByDishIds(ids);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        Page<Dish> page = this.page(new Page<>(dto.getPage(),dto.getPageSize()));
        return new PageResult(page.getTotal(), page.getRecords());
    }
}
